package com.ophiux.cs.infrastructure.repositories.consultBase;

import com.ophiux.cs.domain.consultBase.model.entity.DeptSorts;
import com.ophiux.cs.domain.consultBase.model.valueobj.DeptSortsParentResult;
import com.ophiux.cs.domain.consultBase.model.valueobj.DeptSortsQueryObj;
import com.ophiux.cs.domain.consultBase.model.valueobj.DeptSortsResult;
import com.ophiux.cs.domain.consultBase.repositories.IDeptSortsRepository;
import com.ophiux.cs.infrastructure.compoment.basedata.mybatis.DataComponent;
import com.ophiux.cs.infrastructure.mapper.consultBase.DeptSortsMapper;
import com.ophiux.utils.exception.OphiuxDaoException;
import com.ophiux.utils.exception.OphiuxRepositoryException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class DeptSortsRepository implements IDeptSortsRepository {
	@Autowired
	DeptSortsMapper deptSortsMapper;
	@Autowired
	DataComponent dataComponent;

	@Override
	public void createDeptSorts(DeptSorts deptSorts) throws OphiuxRepositoryException {
		try {
			//TODO 根据医生工号及姓名判断是否存在
			DeptSorts dep=deptSortsMapper.findByName(deptSorts.getSORT_NAME());
			if(dep==null){//不存在，新增
				deptSortsMapper.addDeptSorts(deptSorts);
			}else{//存在，更新
				deptSorts.setSORT_ID(dep.getSORT_ID());
				deptSortsMapper.updateDeptSorts(deptSorts);
			}
		} catch (OphiuxDaoException e) {
			e.printStackTrace();
			log.info("DeptSortsRepository=>createDeptSorts()异常，",e);
			throw new OphiuxRepositoryException("新增医生异常", e);
		}
	}

	@Override
	public void updateDeptSorts(DeptSorts deptSorts) throws OphiuxRepositoryException {
		try {
			deptSortsMapper.updateDeptSorts(deptSorts);
		} catch (OphiuxDaoException e) {
			e.printStackTrace();
			log.info("DeptSortsRepository=>updateDeptSorts()异常，",e);
			throw new OphiuxRepositoryException("更新科室分类异常", e);
		}
	}

	@Override
	public void deleteDeptSorts(String DeptId) throws OphiuxRepositoryException {
		try {
			deptSortsMapper.deleteDeptSorts(DeptId);
		} catch (OphiuxDaoException e) {
			e.printStackTrace();
			log.info("DeptSortsRepository=>deleteDeptSorts()异常，",e);
			throw new OphiuxRepositoryException("删除科室分类异常", e);
		}
	}

	@Override
	public List<DeptSorts> selectDeptSorts() throws OphiuxRepositoryException {
		List<DeptSorts> deptSortsList=null;
		try {
			deptSortsList=deptSortsMapper.selectDeptSorts(null);
		} catch (OphiuxDaoException e) {
			e.printStackTrace();
			log.info("DeptSortsRepository=>selectDeptSorts()异常，",e);
			throw new OphiuxRepositoryException("查询可是分类异常", e);
		}
		return  deptSortsList;
	}

	@Override
	public DeptSorts getDeptSortsByID(String SORT_ID) throws OphiuxRepositoryException {
		DeptSorts deptSorts=null;
		try {
			deptSorts=deptSortsMapper.getDeptSortsById(SORT_ID);
		} catch (OphiuxDaoException e) {
			e.printStackTrace();
			log.info("DeptSortsRepository=>getDeptSortsByID()异常，",e);
			throw new OphiuxRepositoryException("查询科室分类异常", e);
		}
		return  deptSorts;
	}

	@Override
	public List<DeptSortsParentResult> getParentDeptSorts(DeptSortsQueryObj deptSorts) throws OphiuxRepositoryException {
		List<DeptSortsParentResult> deptSortsList=null;
		try {
			deptSortsList=deptSortsMapper.getParentDeptSorts(deptSorts);
		} catch (OphiuxDaoException e) {
			e.printStackTrace();
			log.info("DeptSortsRepository=>getParentDeptSorts()异常，",e);
			throw new OphiuxRepositoryException("查询科室分类异常", e);
		}
		return  deptSortsList;
	}
	@Override
	public  int getParentDeptSortsCount(DeptSortsQueryObj deptSorts) throws OphiuxRepositoryException {
		int count=0;
		try {
			count=deptSortsMapper.getParentDeptSortsCount(deptSorts);
		} catch (OphiuxDaoException e) {
			e.printStackTrace();
			log.info("DeptSortsRepository=>getParentDeptSortsCount()异常，",e);
			throw new OphiuxRepositoryException("查询科室分类异常", e);
		}
		return  count;
	}

	@Override
	public List<DeptSortsResult> getDeptSortsByParentId(String PARENT_DEPT_ID) throws OphiuxRepositoryException{
		List<DeptSortsResult> deptSortsList=null;
		try {
			deptSortsList=deptSortsMapper.getDeptSortsByParentId(PARENT_DEPT_ID);
		} catch (OphiuxDaoException e) {
			e.printStackTrace();
			log.info("DeptSortsRepository=>getDeptSortsByParentId()异常，",e);
			throw new OphiuxRepositoryException("查询科室分类异常", e);
		}
		return  deptSortsList;
	}
}
